package nix.struts.ognl;

import java.util.HashMap;
import java.util.Map;

import nix.struts.ognl.domain.User;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

import org.junit.Test;

public class Demo1 {

	@Test
	public void fun1() throws OgnlException {
		// OGNL准备
		User rootUser = new User("zhangSan", 18);

		Map<String, User> context = new HashMap<String, User>();
		context.put("user1", new User("liSi", 23));
		context.put("user2", new User("wangWu", 30));
		
		OgnlContext oc = new OgnlContext();
		oc.setRoot(rootUser);
		oc.setValues(context);
		// 取root中的值
		String name = (String) Ognl.getValue("name", oc, oc.getRoot());
		Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
		System.out.println(name);
		System.out.println(age);
		// 取context中的值
		String name1 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
		Integer age1 = (Integer) Ognl.getValue("#user1.age", oc, oc.getRoot());
		System.out.println(name1);
		System.out.println(age1);
	}
	
	@Test
	public void fun2() throws OgnlException {
		// OGNL准备
		User rootUser = new User("zhangSan", 18);

		Map<String, User> context = new HashMap<String, User>();
		context.put("user1", new User("liSi", 23));
		context.put("user2", new User("wangWu", 30));
		
		OgnlContext oc = new OgnlContext();
		oc.setRoot(rootUser);
		oc.setValues(context);
		// 设置root中的值
		Ognl.getValue("name='zhaoLiu'", oc, oc.getRoot());
		Ognl.getValue("age=50", oc, oc.getRoot());
		System.out.println(Ognl.getValue("name", oc, oc.getRoot()));
		System.out.println(Ognl.getValue("age", oc, oc.getRoot()));
		// 设置context中的值
		Ognl.getValue("#user2.name='chenBa',#user2.age=44", oc, oc.getRoot());
		System.out.println(Ognl.getValue("#user2.name", oc, oc.getRoot()));
		System.out.println(Ognl.getValue("#user2.age", oc, oc.getRoot()));
	}
	
	@Test
	public void fun3() throws OgnlException {
		// OGNL准备
		User rootUser = new User("zhangSan", 18);

		Map<String, User> context = new HashMap<String, User>();
		context.put("user1", new User("liSi", 23));
		context.put("user2", new User("wangWu", 30));
		
		OgnlContext oc = new OgnlContext();
		oc.setRoot(rootUser);
		oc.setValues(context);
		// 调用root的方法
		Ognl.getValue("setName('zhaoLiu')", oc, oc.getRoot());
		String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());
		System.out.println(name);
		// 调用context中的方法
		String name1 = (String) Ognl.getValue("#user1.setName('chenBa'),#user1.getName()", oc, oc.getRoot());
		System.out.println(name1);		
	}
	
	@Test
	public void fun4() throws OgnlException {
		// OGNL准备
		User rootUser = new User("zhangSan", 18);

		Map<String, User> context = new HashMap<String, User>();
		context.put("user1", new User("liSi", 23));
		context.put("user2", new User("wangWu", 30));
		
		OgnlContext oc = new OgnlContext();
		oc.setRoot(rootUser);
		oc.setValues(context);
		// 调用静态方法
		Object obj = Ognl.getValue("@nix.struts.ognl.TestUtils@echo('hello')", oc, oc.getRoot());
		System.out.println(obj);
		Object pi = Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
		System.out.println(pi);
		Object pi2 = Ognl.getValue("@@PI", oc, oc.getRoot());
		System.out.println(pi2);
	}
	
	@Test
	public void fun5() throws OgnlException {
		// OGNL准备
		User rootUser = new User("zhangSan", 18);

		Map<String, User> context = new HashMap<String, User>();
		context.put("user1", new User("liSi", 23));
		context.put("user2", new User("wangWu", 30));
		
		OgnlContext oc = new OgnlContext();
		oc.setRoot(rootUser);
		oc.setValues(context);
		// 创建、操作List
		Integer size = (Integer) Ognl.getValue("{'zhangSan','liSi','wangWu','zhaoLiu'}.size()", oc, oc.getRoot());
		System.out.println(size);
		String name1 = (String) Ognl.getValue("{'zhangSan','liSi','wangWu','zhaoLiu'}[2]", oc, oc.getRoot());
		System.out.println(name1);
		String name2 = (String) Ognl.getValue("{'zhangSan','liSi','wangWu','zhaoLiu'}.get(1)", oc, oc.getRoot());
		System.out.println(name2);
	}
	
	@Test
	public void fun6() throws OgnlException {
		// OGNL准备
		User rootUser = new User("zhangSan", 18);

		Map<String, User> context = new HashMap<String, User>();
		context.put("user1", new User("liSi", 23));
		context.put("user2", new User("wangWu", 30));
		
		OgnlContext oc = new OgnlContext();
		oc.setRoot(rootUser);
		oc.setValues(context);
		// 创建、操作Map
		Integer size = (Integer) Ognl.getValue("#{'name':'zhangSan','age':18}.size()", oc, oc.getRoot());
		System.out.println(size);
		String name1 = (String) Ognl.getValue("#{'name':'zhangSan','age':18}['name']", oc, oc.getRoot());
		System.out.println(name1);
		Integer age1 = (Integer) Ognl.getValue("#{'name':'zhangSan','age':18}.get('age')", oc, oc.getRoot());
		System.out.println(age1);
	}
}
